import MessageBoxVue from './src/MessageBox.vue';

import { Dialog } from '@/components/Dialog';

let msgId = 0;

/**
 * @description 消息弹框
 * @param {string} text 提示信息
 * @param {array} offset 定位
 * @param {string} confirmText 确定按钮文字
 * @param {string} cancelText 取消按钮文字
 * @param {string} iconName 图标名
 * @param {string} title 提示标题
 * @param {string} tip 建议信息
 * @return Promise
 */
function MessageBox(config): Promise<boolean> {
  msgId++;
  const MsgBox = 'MessageBox';
  const { title, text, offset, confirmText, cancelText, iconName, zIndexDefault, tip } = config;
  const cid = MsgBox + (msgId - 1);
  const dialog = Dialog(cid);

  dialog?.close(cid);

  return new Promise((resolve) => {
    Dialog({
      cid: MsgBox + msgId,
      content: MessageBoxVue,
      data: {
        text,
        title,
        iconName,
        confirmText,
        cancelText,
        tip,
        onConfirm: () => {
          config.onConfirm?.();
          resolve(true);
        },
        onCancel: () => {
          resolve(false);
        },
      },
      zIndexDefault,
      offset: offset,
      shade: true,
    });
  });
}

export default MessageBox;
